from __future__ import annotations

import datetime

import arcade
import game.config as config
from game.config import *

# from game.main import test_main
from game.utils.grid_util import grid_parser


class SnakeEntity:

    def __init__(self):
        # 元组数组，表示坐标
        # 默认头部在末尾
        self.body=[(0,0),(0,1),(0,2)]
        self.direction=config.DIRECTION_RIGHT
        

        pass

    def change_direction(self,direction):
        # 变更方向
        self.direction=direction

    def next_position(self):
        # 计算下一个位置
        end_position=self.body[-1]
        if self.direction==DIRECTION_RIGHT:
            new_position=(end_position[0],end_position[1]+1)
        elif self.direction==DIRECTION_LEFT:
            new_position=(end_position[0],end_position[1]-1)
        elif self.direction==DIRECTION_UP:
            new_position=(end_position[0]-1,end_position[1])
        else:
            new_position=(end_position[0]+1,end_position[1])
        return new_position


    def move(self,with_eat=False):
        

        # 移动(计算下一个位置)
        new_position=self.next_position()

        # 计算下一个位置是否合法
        new_r,new_c=new_position
        position_legal=0<=new_r<ROW_COUNT and 0<=new_c<COL_COUNT
        if not position_legal:
            print(f'not legal:{new_position}')
            raise Exception('wall')
        
        # 判断下一个位置是否是自己的身体
        if new_position in self.body:
            raise Exception('body')

        # 判断下个位置是否有食物
        if with_eat:
            self.body=self.body+[new_position]
        else:
            self.body=self.body[1:]+[new_position]
        pass

    def draw(self):
        for t in self.body:
            (r,c)=t
            x,y=grid_parser.calc_xy_by_rc(r,c)
            arcade.draw_circle_filled(x,y,config.CELL_LEN//2,arcade.color.BLUE_BELL)

        pass

# if __name__=='__main__':
#     test_main()
#     pass